﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Fetion.Core
{

    /// <summary>
    /// byte转换器,对基础类型byte之间转换的实现
    /// </summary>
    public interface IByteConverter
    {
        /// <summary>
        /// 获取一个char的byte[]
        /// </summary>
        /// <param name="value">char</param>
        /// <returns>byte[]</returns>
        byte[] GetBytes(char value);
        /// <summary>
        /// 获取一个int16的byte[]
        /// </summary>
        /// <param name="value">int16</param>
        /// <returns>byte[]</returns>
        byte[] GetBytes(short value);
        /// <summary>
        /// 获取到一个int32的byte[]
        /// </summary>
        /// <param name="value">int32</param>
        /// <returns>byte[]</returns>
        byte[] GetBytes(int value);
        /// <summary>
        /// 获取一个int64的byte[]
        /// </summary>
        /// <param name="value">int64</param>
        /// <returns>byte[]</returns>
        byte[] GetBytes(long value);
        /// <summary>
        /// 获取一个float的byte[]
        /// </summary>
        /// <param name="value">float</param>
        /// <returns>byte[]</returns>
        byte[] GetBytes(float value);
        /// <summary>
        /// 获取一个double的byte[]
        /// </summary>
        /// <param name="value">double</param>
        /// <returns>byte[]</returns>
        byte[] GetBytes(double value);
        /// <summary>
        /// 获取一个byte的byte[]
        /// </summary>
        /// <param name="value">byte</param>
        /// <returns>byte[]</returns>
        byte[] GetBytes(byte value);
        /// <summary>
        /// 获取一个bool的byte[]
        /// </summary>
        /// <param name="value">bool</param>
        /// <returns>byte[]</returns>
        byte[] GetBytes(bool value);
        /// <summary>
        /// 从流中获取char
        /// </summary>
        /// <param name="reader">流读取对象</param>
        /// <returns>char</returns>
        char ReadChar(BufferReader reader);
        /// <summary>
        /// 从流中获取int16
        /// </summary>
        /// <param name="reader">流读取对象</param>
        /// <returns>short</returns>
        short ReadInt16(BufferReader reader);
        /// <summary>
        /// 从流中获取int32
        /// </summary>
        /// <param name="reader">流读取对象</param>
        /// <returns>int</returns>
        int ReadInt32(BufferReader reader);
        /// <summary>
        /// 从流中获取int64
        /// </summary>
        /// <param name="reader">流读取对象</param>
        /// <returns>long</returns>
        long ReadInt64(BufferReader reader);
        /// <summary>
        /// 从流中获取float
        /// </summary>
        /// <param name="reader">流读取对象</param>
        /// <returns>float</returns>
        float ReadFloat(BufferReader reader);
        /// <summary>
        /// 从流中获取double
        /// </summary>
        /// <param name="reader">流读取对象</param>
        /// <returns>double</returns>
        double ReadDouble(BufferReader reader);
        /// <summary>
        /// 从流中获取byte
        /// </summary>
        /// <param name="reader">流读取对象</param>
        /// <returns>byte</returns>
        byte ReadByte(BufferReader reader);
        /// <summary>
        /// 从流中获取bool
        /// </summary>
        /// <param name="reader">流读取对象</param>
        /// <returns>bool</returns>
        bool ReadBool(BufferReader reader);
    }
    class ByteConverter : IByteConverter
    {

        private byte[] m1byteData = new byte[1];
        private byte[] m2byteData = new byte[2];
        private byte[] m4byteData = new byte[4];
        private byte[] m8byteData = new byte[8];
        public  byte[] GetBytes(char value)
        {
            return BitConverter.GetBytes(value);
        }
        public  byte[] GetBytes(short value)
        {
            return BitConverter.GetBytes(value);
        }
        public  byte[] GetBytes(int value)
        {
            return BitConverter.GetBytes(value);
        }
        public  byte[] GetBytes(long value)
        {
            return BitConverter.GetBytes(value);
        }
        public  byte[] GetBytes(float value)
        {
            return BitConverter.GetBytes(value);
        }
        public  byte[] GetBytes(double value)
        {
            return BitConverter.GetBytes(value);
        }
        public byte[] GetBytes(byte value)
        {
            return new byte[] { value };

        }
        public byte[] GetBytes(bool value)
        {
            return BitConverter.GetBytes(value);
        }
        private ByteArraySegment mBAS;
        public char ReadChar(BufferReader reader)
        {
            mBAS = reader.Read(2);
            return BitConverter.ToChar(mBAS.Array, mBAS.Offset);
        }
        public short ReadInt16(BufferReader reader)
        {
            mBAS = reader.Read(2);
            return BitConverter.ToInt16(mBAS.Array, mBAS.Offset);
        }
        public int ReadInt32(BufferReader reader)
        {
            mBAS = reader.Read(4);
            return BitConverter.ToInt32(mBAS.Array, mBAS.Offset);
        }
        public long ReadInt64(BufferReader reader)
        {
            mBAS = reader.Read(8);
            return BitConverter.ToInt64(mBAS.Array, mBAS.Offset);
        }
        public float ReadFloat(BufferReader reader)
        {
            mBAS = reader.Read(4);
            return BitConverter.ToSingle(mBAS.Array, mBAS.Offset);
        }
        public double ReadDouble(BufferReader reader)
        {
            mBAS = reader.Read(8);
            return BitConverter.ToDouble(mBAS.Array, mBAS.Offset);
        }
        public byte ReadByte(BufferReader reader)
        {
            mBAS = reader.Read(1);
            return mBAS.Array[mBAS.Offset];
        }
        public bool ReadBool(BufferReader reader)
        {
            mBAS = reader.Read(1);
            return BitConverter.ToBoolean(mBAS.Array, mBAS.Offset);
        }
    }

}
